using System;
using System.Collections.Generic;
using System.Text;
using Chilkat;

namespace Hermes.Framework.FileSystem.Ftp
{
    public class FtpFileSystem : FtpFileSystemTemplate
    {
        protected Ftp2 ftp = null;

        public FtpFileSystem()
        {

        }

        public override void Connect(
            ConnectionInfo connectionInfo)
        {
            bool success;
            ftp = new Ftp2();

            success = ftp.UnlockComponent("Anything for 30-day trial");
            // Any string unlocks the component for the 1st 30-days.
            if (success != true)
            {
                throw new ApplicationException("The class cannot be unlocked!");
            }

            ftp.Hostname = connectionInfo.Url;
            ftp.Port = connectionInfo.Port;
            ftp.Username = connectionInfo.User;
            ftp.Password = connectionInfo.Password;

            success = ftp.Connect();
            Assert(success);
        }

        protected void Assert(bool success)
        {
            if (success == false)
            {
                Console.WriteLine(this.ftp.LastErrorText);
                throw new Exception(this.ftp.LastErrorText);
            }
        }

        public override void ChangeRemoteDirectory(string directoryName)
        {
            bool success = this.ftp.ChangeRemoteDir("/" + directoryName);
            Assert(success);
        }

        public override void PutFile(string from, string to)
        {
            bool success = ftp.PutFile(from, to);
            Assert(success);
        }

        public override void GetFile(string from, string to)
        {
            bool success = ftp.GetFile(from, to);
            Assert(success);
        }

        public override void DoRename(string from, string to)
        {
            Url fromUrl = new Url(from);
            Url toUrl = new Url(to);

            bool success = this.ftp.RenameRemoteFile(fromUrl.File, toUrl.File);
            Assert(success);
        }

        public override void DoDelete(string fileName)
        {
            bool success = this.ftp.DeleteRemoteFile(fileName);
            Assert(success);
        }

        public override bool IsExisted(string fileName)
        {
            return this.ftp.GetSizeByName(fileName) >= 0;
        }

        public override void DoCreateDirectory(string directoryName)
        {
            this.ftp.CreateRemoteDir(directoryName);
        }

        public override void DoDeleteDirectory(string directoryName)
        {
            this.ftp.DeleteTree();
        }

        public override int GetNumberOfItems(string directoryName)
        {
            return this.ftp.NumFilesAndDirs;
        }

        public override string GetItemName(string directoryName, int index)
        {
            return this.ftp.GetFilename(index);
        }

        public override void DoRenameDirectory(string from, string to)
        {
            this.Rename(from, to);
        }

        public override void Dispose()
        {
            this.ftp.Disconnect();
            this.ftp.Dispose();
        }
    }
}
